package org.rjxh.examfront.pojo;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import java.io.Serializable;
import java.util.List;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import jdk.jfr.Timestamp;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

/**
 * <p>
 * 考试考题
 * </p>
 *
 * @author BA7LGJ
 * @since 2024-04-04
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("exam_question")
@ApiModel(value="ExamQuestion对象", description="考试考题")
public class ExamQuestion implements Serializable {

    private static final long serialVersionUID = 1L;
    public ExamQuestion(Long questionId, String questionContent, String questionType, String image, Long examId, Integer order) {
        this.questionId = questionId;
        this.questionContent = questionContent;
        this.questionType = questionType;
        this.image = image;
        this.examId = examId;
        this.order = order;
    }

    public ExamQuestion(ExamQuestion examQuestion){
        this.questionId = examQuestion.questionId;
        this.questionContent = examQuestion.questionContent;
        this.questionType = examQuestion.questionType;
        this.image = examQuestion.image;
        this.examId = examQuestion.examId;
        this.order = examQuestion.order;
        this.examAnswers = examQuestion.examAnswers;
    }
    @ApiModelProperty(value = "问题ID")
    @TableId(value = "question_id", type = IdType.ASSIGN_ID)
    private Long questionId;

    @ApiModelProperty(value = "问题内容")
    @TableField("question_content")
    private String questionContent;

    public void setQuestionContent(String questionContent) {
        if (questionType!=null && questionType.equals("c"))
        {
            this.examQuestionCode = JSON.parseObject(questionContent, ExamQuestionCode.class);
        }
        this.questionContent = questionContent;
    }

    public String getQuestionContent() {
        if (questionType.equals("c"))
        {
            try {
                //如果examQuestionCode以及其内部的文本没问题，则直接返回
                if (!(examQuestionCode !=null
                        && examQuestionCode.getBaseCode()!=null
                        && examQuestionCode.getDescribe()!=null
                        && examQuestionCode.getUmpireCode()!=null)) {
                    this.examQuestionCode = JSON.parseObject(questionContent, ExamQuestionCode.class);
                }
                return examQuestionCode.toJson();
            } catch (Exception e) {
                return questionContent;
            }
        }
        return questionContent;
    }

    @ApiModelProperty(value = "问题类型")
    @TableField("question_type")
    private String questionType;

    @ApiModelProperty(value = "图片路径")
    @TableField("image")
    private String image;

    @ApiModelProperty(value = "考试ID")
    @TableField("exam_id")
    private Long examId;

    @ApiModelProperty(value = "排序")
    @TableField("`order`")
    private Integer order;

    @TableField(exist = false)
    private List<ExamAnswer> examAnswers;

    @TableField(exist = false)
    private ExamQuestionCode examQuestionCode;


    @TableField(exist = false)
    private ExamRespond examRespond;

    public ExamQuestionCode getExamQuestionCode() {
        if (examQuestionCode==null){
            this.examQuestionCode = new ExamQuestionCode();
        }
        return examQuestionCode;
    }

    public void reflectCode(){
        this.examQuestionCode = JSON.parseObject(questionContent, ExamQuestionCode.class);
    }



}
